package com.loie.xcx.web.controller;


import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.aop.SignatureValidation;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.model.bo.CorporateCustomers;
import com.loie.xcx.model.bo.PurchaseOrders;
import com.loie.xcx.model.bo.PurchaseOrdersLine;
import com.loie.xcx.model.dto.IwinstanceQueryDTO;
import com.loie.xcx.model.dto.PurchaseOrdersCommitDTO;
import com.loie.xcx.service.CorporateCustomersService;
import com.loie.xcx.service.PurchaseOrdersService;
import com.loie.xcx.util.AttachmentSave;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-10-03
 */
@RestController
@RequestMapping("/purchaseorders")
@Slf4j
public class PurchaseOrdersController {
    @Autowired
    PurchaseOrdersService purchaseOrdersService;

    @Autowired
    CorporateCustomersService corporateCustomersService;
    @Autowired
    private
    AttachmentSave attachmentSave;

    @SignatureValidation
    @PostMapping("/getOrdersDetail")
    public JsonResult<PurchaseOrdersCommitDTO> getQuotationLineList(@RequestBody String jsonStr) {
        IwinstanceQueryDTO quotationQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (StringUtils.isNotBlank(quotationQueryDTO.getRecordId()) && quotationQueryDTO.getPageIndex() != null) {
            List<PurchaseOrders> purchaseOrdersList = purchaseOrdersService.getPurchaseOrdersList(quotationQueryDTO);
            if (purchaseOrdersList.size()>0) {
                PurchaseOrdersCommitDTO purchaseOrdersCommitDTO = new PurchaseOrdersCommitDTO();
                PurchaseOrders purchaseOrders=purchaseOrdersList.get(0);
                BeanUtils.copyProperties(purchaseOrders, purchaseOrdersCommitDTO);
                List<PurchaseOrdersLine> purchaseOrdersLines = purchaseOrdersService.getPurchaseOrdersLineList(quotationQueryDTO);
                int size = purchaseOrdersLines.size();
                purchaseOrdersCommitDTO.setItemList(purchaseOrdersLines);
                purchaseOrdersCommitDTO.setTotal(size);
                return new JsonResult<>(purchaseOrdersCommitDTO);
            } else {
                PurchaseOrdersCommitDTO purchaseOrdersCommitDTO = new PurchaseOrdersCommitDTO();
                return new JsonResult<>(purchaseOrdersCommitDTO);
            }
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/getCorporateCustomersdetail")
    public JsonResult<CorporateCustomers> getCorporateCustomersdetail(@RequestBody String jsonStr) {
        IwinstanceQueryDTO quotationQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
        if (StringUtils.isNotBlank(quotationQueryDTO.getRecordId()) && quotationQueryDTO.getPageIndex() != null) {
            List<CorporateCustomers> corporateCustomersList = corporateCustomersService.getCorporateCustomersList(quotationQueryDTO);
            if (corporateCustomersList.size()>0) {
                //PurchaseOrdersCommitDTO purchaseOrdersCommitDTO = new PurchaseOrdersCommitDTO();
                CorporateCustomers corporateCustomers=corporateCustomersList.get(0);
                //BeanUtils.copyProperties(purchaseOrders, purchaseOrdersCommitDTO);

                return new JsonResult<>(corporateCustomers);
            } else {
                CorporateCustomers corporateCustomers = new CorporateCustomers();
                return new JsonResult<>(corporateCustomers);
            }
        }
        return new JsonResult<>(400, "查询失败");
    }

    //@SignatureValidation
//    @PostMapping("/getAttachmentList")
//    public  JsonResult<AttachmentListDTO> AttachmentList(@RequestBody String jsonStr) {
//        IwinstanceQueryDTO iwinstanceQueryDTO = JSONObject.parseObject(jsonStr, IwinstanceQueryDTO.class);
//        if (StringUtils.isNotBlank(iwinstanceQueryDTO.getRecordId()) &&
//                StringUtils.isNotBlank(iwinstanceQueryDTO.getModule())) {
//            List<AttachmentDTO> attachmentDTOList=attachmentSave.attachmenttobase64(iwinstanceQueryDTO);
//            AttachmentListDTO attachmentListDTO=new  AttachmentListDTO();
//            attachmentListDTO.setTotal(attachmentDTOList.size());
//            attachmentListDTO.setAttachmentList(attachmentDTOList);
//            //System.out.println(attachmentListDTO);
//            log.info(attachmentListDTO.toString());
//            return new JsonResult<>(attachmentListDTO);
//        }
//
//        return new JsonResult<>(400, "查询附件失败");
//
//
//        }


    //@SignatureValidation


}

